//
//  ZCSTipsRecomdViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "ZCSTipsRecomdViewController.h"
#import "ZCSTipsReTableViewCell.h"
#import "ZCSEditorReListModel.h"
#import "AlbumDetailViewController.h"
#import "LTAlbumModel.h"
#import "CBStoreHouseRefreshControl.h"

@interface ZCSTipsRecomdViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArray;
//页码
@property (nonatomic, assign) NSInteger page;

@property (nonatomic, assign) NSInteger maxPage;

@property (nonatomic, strong) CBStoreHouseRefreshControl *storeHouseRefreshControl;

@property (nonatomic, retain) NSMutableArray *collectArray;

@end

@implementation ZCSTipsRecomdViewController
- (void)dealloc
{
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = _titleString;
    self.dataArray = [NSMutableArray array];
    self.collectArray = [NSMutableArray array];
    [self creatTableView];
    [self getDataWithPage:1 Keyword:self.keywordId];
    self.page = 1;
    self.navigationController.navigationBar.translucent = YES;
    
    
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSTipsReTableViewCell" bundle:nil] forCellReuseIdentifier:@"TipCell"];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self getMoreData];
    }];
    self.storeHouseRefreshControl = [CBStoreHouseRefreshControl attachToScrollView:self.tableView target:self refreshAction:@selector(refreshTriggered:) plist:@"LTFM" color:[UIColor blackColor] lineWidth:3 dropHeight:80 scale:1.2 horizontalRandomness:300 reverseLoadingAnimation:NO internalAnimationFactor:0.7];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.storeHouseRefreshControl scrollViewDidScrollWithType:1];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.storeHouseRefreshControl scrollViewDidEndDragging];
}

- (void)refreshTriggered:(id)sender
{
    
    [self performSelector:@selector(finishRefreshControl) withObject:nil afterDelay:2 inModes:@[NSRunLoopCommonModes]];
}

- (void)finishRefreshControl
{
    [self.dataArray removeAllObjects];
    self.page = 1;
    [self getDataWithPage:self.page Keyword:self.keywordId];
}

- (void)getMoreData
{
    self.page++;
    if (self.page > self.maxPage) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    else
    {
        [self getDataWithPage:self.page Keyword:self.keywordId];
    }
}

- (void)getDataWithPage:(NSInteger)page Keyword:(NSInteger)keyId
{
    NSString *urlString;
    if (keyId) {
        urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/keyword/albums?calcDimension=hot&categoryId=%ld&device=iPhone&keywordId=%ld&pageId=%ld&pageSize=20&status=0", self.cId, keyId, page];
    }
    else
    {
     urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=android&pageId=%ld&pageSize=20&statEvent=pageview%%2Falbumlist%40%E5%%B0%%8F%%E7%%BC%96%E6%%8E%%A8%%E8%%8D%%90&statModule=%%E5%%B0%%8F%%E7%%BC%96%E6%%8E%%A8%%E8%%8D%%90_%%E6%%9B%%B4%%E5%%A4%%9A&statPage=tab%40%E5%%8F%91%E7%%8E%%B0_%%E6%%8E%%A8%%E8%%8D%%90", page];
    }
    __weak ZCSTipsRecomdViewController *trvc = self;
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:0 Success:^(id result) {
        NSArray *tempArray = result[@"list"];
        trvc.maxPage = [result[@"maxPageId"] integerValue];
        for (NSDictionary *dic in tempArray) {
            ZCSEditorReListModel *model = [[ZCSEditorReListModel alloc] initWithDic:dic];
            LTAlbumModel *albumModel = [[LTAlbumModel alloc] initWithDic:dic];
            [trvc.collectArray addObject:albumModel];
            [trvc.dataArray addObject:model];
        }
        [trvc.tableView reloadData];
        [trvc.tableView.mj_footer endRefreshing];
        [trvc.storeHouseRefreshControl finishingLoading];
    } Failure:^(NSError *error) {
        [trvc.tableView.mj_footer endRefreshing];
        NSLog(@"%@", error);
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZCSTipsReTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TipCell"];
    cell.model = [self.dataArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZCSEditorReListModel *model = [self.dataArray objectAtIndex:indexPath.row];
    AlbumDetailViewController *advc = [[AlbumDetailViewController alloc] init];
    advc.albmId = model.albumId;
    advc.albumModel = [self.collectArray objectAtIndex:indexPath.row];
    advc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:advc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * FITHEIGHT;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
